package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.util.IpUtil;
import com.g2room.myuall.grap.univ.po.ImgPo;
import com.g2room.myuall.grap.univ.po.UnivInfoExtPo;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapZdx implements Runnable {
	//http://www.youzy.cn/college/search?page=146
	static String pageUrl = "http://www.youzy.cn/college/search?page="; 
	static PreparedStatement  errPs = null;
	static String baseUrl = "http://www.youzy.cn";
	static String[] cats = {"校园设施","校园景观","文化生活"};
	
	static List<String> imgPages = new ArrayList<String>();
	static {
//		imgPages.add("http://www.youzy.cn/college/3470/photos?type=1&tag=校园设施&page=2");
//		imgPages.add("http://www.youzy.cn/college/2312/photos?type=1&tag=");
//		imgPages.add("http://www.youzy.cn/college/2172/photos?type=1&tag=");
//		imgPages.add("http://www.youzy.cn/college/3978/photos?type=1&tag=");
//		imgPages.add("http://www.youzy.cn/college/2628/photos?type=1&tag=");
//		imgPages.add("http://www.youzy.cn/college/1806/photos?type=1&tag=校园景观&page=2");
//		imgPages.add("http://www.youzy.cn/college/3141/photos?type=1&tag=");
//		imgPages.add("http://www.youzy.cn/college/3010/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/883/photos?type=1&tag= ");
		imgPages.add("http://www.youzy.cn/college/1501/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/2688/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/3091/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1260/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/904/photos?type=1&tag= ");
		imgPages.add("http://www.youzy.cn/college/3437/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/2614/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1015/photos?type=1&tag=校园设施&page=2");
		imgPages.add("http://www.youzy.cn/college/1163/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1902/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/2476/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/2437/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/2784/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/3033/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1957/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/3709/photos?type=1&tag=文化生活&page=2");
		imgPages.add("http://www.youzy.cn/college/1516/photos?type=1&tag=校园设施&page=2");
		imgPages.add("http://www.youzy.cn/college/1799/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1205/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1375/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1606/photos?type=1&tag=文化生活&page=2");
		imgPages.add("http://www.youzy.cn/college/2810/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1349/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1885/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/2773/photos?type=1&tag=");
		imgPages.add("http://www.youzy.cn/college/1347/photos?type=1&tag=校园设施&page=2");
	}
	
	public static void main(String[] args) {
		GrapZdx gue = new GrapZdx();
		Document doc = null;
		// 补抓数据
		for(String imgUrl : imgPages) {
			System.out.println("url==" + imgUrl);
			Integer sid = Integer.valueOf(imgUrl.replace("http://www.youzy.cn/college/", "").substring(0, imgUrl.replace("http://www.youzy.cn/college/", "").indexOf("/")));
			
			if(imgUrl.contains("page=2")) {
				try {
					String tag = imgUrl.substring(imgUrl.indexOf("tag=") + 4, imgUrl.indexOf("&page=2"));
					ImgPo imgPo = new ImgPo();
					imgPo.setCategory(tag);
					imgPo.setSchoolId(sid);
					
					doc = new GenericMyuall().getDoc(imgUrl);
					gue.grapImgs(doc, imgPo);
				} catch (IOException e) {
					e.printStackTrace();
					gue.insert2ErrDb(imgUrl, 3);
				}
			} else {
				UnivInfoExtPo ip = new UnivInfoExtPo();
				ip.setSchoolId(sid);
				gue.getImgs(ip, imgUrl);
			}
		}
		
		// 以下为抓取
//		for(int i =0; i<15; i++) {
//			try {
//				Thread.sleep(100);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			GrapZdx gue = new GrapZdx();
//			Thread t = new Thread(gue);
//			t.start();
//		}
	}
	

	@Override
	public void run() {
		List<String> listUrls = getUrl();
		if(null == listUrls) return;
		for(String url : listUrls) {
			getList(url);
		}
	}
	
	public static AtomicInteger page = new AtomicInteger(1);
	private static synchronized List<String> getUrl() {
		if(page.intValue() == 147) return null;
		List<String> listUrls = new ArrayList<String>();
		for(int i = page.intValue(); i< page.intValue() + 10; i++) {
			if(i>146) break;
			listUrls.add(pageUrl+i);
		}
		page.addAndGet(10);
		return listUrls;
	}

	
	/**
	 * 获取分页列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String url) {
		System.out.println("start get page data!" + url);
		Document doc = null;
		try {
			doc = new GenericMyuall().getDoc(url);
		} catch (IOException e1) {
			e1.printStackTrace();
			insert2ErrDb(url, 1);
			return;
		}
		if(null == doc) return;
		
//		Elements uls = doc.getElementsByAttributeValue("class", "uzy-college-list");
		Elements lis = doc.select("ul.uzy-college-list > li");
		List<UnivInfoExtPo> univs = new ArrayList<UnivInfoExtPo>();
		for(Element li : lis) {
			UnivInfoExtPo univ = new UnivInfoExtPo();
			
			Element markEl = li.getElementsByAttributeValue("class","mark").first();
			String badge = markEl.getElementsByTag("img").first().attr("src");
			univ.setBadge(badge);
			Element infoEl = li.getElementsByAttributeValue("class","info").first();
			String name = infoEl.getElementsByAttributeValue("class", "name").first().text();
			univ.setSchoolname(name);
			Elements tagEls = infoEl.getElementsByAttributeValue("class", "tag").first().getElementsByTag("img");
			if(null != tagEls) {
				for(Element tagEl : tagEls) {
					String title = tagEl.attr("title");
					if(title.equals("卓越")) {
						univ.setIsExcellent(1);
					} else if(title.equals("省重点")) {
						univ.setIsProvKey(1);
					}
				}
			}
			Elements els = li.getElementsByAttributeValue("class","bottom").last().getElementsByTag("img");
			if(null != els) {
				for(Element el : els) {
					String title = el.attr("title");
					if(title.equals("艺术生")) {
						univ.setIsArt(1);
					} else if(title.equals("单招生")) {
						univ.setIsSingle(1);
					}
				}
			}
			// 获取隶属于
			Element belongEl = li.getElementsByAttributeValue("class","quarter_1").first();
			String belong = belongEl.text();
			if(null != belong) {
				univ.setBelong(belong.replace("隶属：", ""));
			}
			
			// 获取学校ID
			Element el = li.getElementsByTag("input").first();
			univ.setSchoolId(Integer.valueOf(el.val()));
			univs.add(univ);
		}
        
        for(UnivInfoExtPo univ : univs) {
        	try {
				Thread.sleep(IpUtil.genRandomNum() * 500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
        	insert2Db(univ);
        	String imgUrl = "http://www.youzy.cn/college/"+univ.getSchoolId()+"/photos?type=1&tag=";
        	getImgs(univ, imgUrl);
        }
	}

	
	/**
	 * 获取列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getImgs(UnivInfoExtPo univ, String imgUrl) {
		for(String tag : cats) {
			ImgPo imgPo = new ImgPo();
			imgPo.setCategory(tag);
			imgPo.setSchoolId(univ.getSchoolId());
			String imgPageUrl = imgUrl + tag;
			Document doc = null;
			try {
				doc = new GenericMyuall().getDoc(imgPageUrl);
			} catch (IOException e1) {
				e1.printStackTrace();
				insert2ErrDb(imgUrl, 2);
				return;
			}
			grapImgs(doc, imgPo);
			// 获取分页信息
			Elements pageEls = doc.getElementsByAttributeValue("class", "pagination ").first().getElementsByTag("li");
			if(null != pageEls && pageEls.size() > 3) {
				// 直接取倒数第二个li的值
				Element liEl = pageEls.get(pageEls.size() - 2);
				int maxPage = Integer.valueOf(liEl.text());
				for(int page = 2; page <= maxPage; page++) {
					try {
						doc = new GenericMyuall().getDoc(imgPageUrl + "&page=" + page);
						grapImgs(doc, imgPo);
					} catch (IOException e) {
						e.printStackTrace();
						insert2ErrDb(imgPageUrl + "&page=" + page, 3);
					}
				}
			}
		}
	}
	
	private void grapImgs(Document doc, ImgPo imgPo) {
		try {
			// 获取图片li
			Elements liEls = doc.getElementsByAttributeValue("class", "container page-photo-list").first().getElementsByTag("li");
			for(Element li : liEls) {
				// 获取图片和名称
				Element div = li.getElementsByAttributeValue("class", "fancybox-thumbs").last();
				imgPo.setType(div.text());
				String url = div.attr("href");
				imgPo.setSmallUrl(url.replace(".jpeg","_190.jpeg"));
				imgPo.setUrl(url);
				insert2ImgDb(imgPo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void insert2Db(UnivInfoExtPo univ) {
		try {
			PreparedStatement univPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_info_ext1` ("
					+ "`schoolname`, `belong`, `isSingle`, `isArt`, `isProvKey`, `isExcellent`, `badge`, `schoolId`) "
					+ "values(?,?,?,?,?,?,?,?)");
			univPs.setString(1, univ.getSchoolname());
			univPs.setString(2, univ.getBelong());
			univPs.setInt(3, univ.getIsSingle());
			univPs.setInt(4, univ.getIsArt());
			univPs.setInt(5, univ.getIsProvKey());
			univPs.setInt(6, univ.getIsExcellent());
			univPs.setString(7, univ.getBadge());
			univPs.setInt(8, univ.getSchoolId());
			univPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	

	
	int sqlDetailCount = 0;
	public void insert2ImgDb(ImgPo imgPo) {
		try {
			PreparedStatement  majorPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_images` ("
					+ "`schoolId`, `category`, `type`,`smallUrl`, `url`) "
				+ "values(?,?,?,?,?)");
			majorPs.setInt(1, imgPo.getSchoolId());
			majorPs.setString(2, imgPo.getCategory());
			majorPs.setString(3, imgPo.getType());
			majorPs.setString(4, imgPo.getSmallUrl());
			majorPs.setString(5, imgPo.getUrl());
			majorPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	int sqlErrCount = 0;
	public void insert2ErrDb(String url, int type) {
		try {
			if(sqlDetailCount == 0) {
				errPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_err` (`url`, `type`) "
					+ "values(?,?)");
			}
			errPs.setString(1, url);
			errPs.setInt(2, type);
			errPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public Map<String, Integer> getErrs() {
		Map<String, Integer> errs = new HashMap<String, Integer>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select * from univ_err";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				errs.put(rs.getString("url"), rs.getInt("type"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return errs;
	}
	
	

}
